package com.shangcheng.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shangcheng.dubbo.api.SmsHomeAdvertiseApi;
import com.shangcheng.model.domain.SmsHomeAdvertise;
import com.shangcheng.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SmsHomeAdvertiseService {

    @DubboReference
    private SmsHomeAdvertiseApi smsHomeAdvertiseApi;

    /**
     * 添加广告
     *
     * @param smsHomeAdvertise
     */
    public void create(SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseApi.save(smsHomeAdvertise);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    public void delete(List<Integer> ids) {
        smsHomeAdvertiseApi.delete(ids);
    }

    /**
     * 分页查询广告
     *
     * @param endTime
     * @param name
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    public PageResult findList(String endTime, String name, Integer pageNum, Integer pageSize, Integer type) {
        //1.得到IPage对象
        IPage page = smsHomeAdvertiseApi.findList(endTime, name, pageNum, pageSize, type);
        //2.设置PageResult对象
        PageResult pr = new PageResult();
        //3.进行数据的封装
        //3.封装PageResult
        pr.setList(page.getRecords());
        pr.setTotal(Convert.toInt(page.getTotal()));
        pr.setPageNum(pageNum);
        pr.setPageSize(pageSize);
        pr.setTotalPage(Convert.toInt(page.getPages()));
        return pr;
    }

    /**
     * 修改上下线状态
     *
     * @param id
     * @param status
     */
    public void updateStatus(Integer id, Integer status) {
        //1.根据id进行数据的查询
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseApi.findById(id);
        //2.判断是否存在,如果不存在直接结束
        if (ObjectUtil.isEmpty(smsHomeAdvertise)) return;
        //3.如果存在就进行修改
        smsHomeAdvertise.setStatus(status);
        smsHomeAdvertiseApi.update(smsHomeAdvertise);
    }

    /**
     * 修改广告
     *
     * @param smsHomeAdvertise
     */
    public void update(SmsHomeAdvertise smsHomeAdvertise) {
        if (ObjectUtil.isEmpty(smsHomeAdvertise)) return;
        smsHomeAdvertiseApi.update(smsHomeAdvertise);
    }

    /**
     * 获取广告详情
     *
     * @param id
     * @return
     */
    public SmsHomeAdvertise findById(Integer id) {
        return smsHomeAdvertiseApi.findById(id);
    }
}
